package com.wp.hihocoder;

import java.util.Scanner;

/**
 * Created by 王萍 on 2017/8/22 0022.
 */
public class 有序01字符串 {

    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int number = sc.nextInt();
        if (number < 1 || number > 10) return;
        String[] strings = new String[number];
        int[] modifyNumber = new int[number];
        for (int i = 0; i < number; i++) {
            strings[i] = sc.next();
            modifyNumber[i] = getModifyNumber2(strings[i]);
        }
        for (int i = 0; i < number; i++) {
            System.out.println(modifyNumber[i]);
        }
    }

    //通过率百分之四十
    public static int getModifyNumber(String string) {
        char[] stringChars = string.toCharArray();
        boolean startPlus1To0 = false;
        boolean startPlus0To1 = false;
        int modifyNumber1To0 = 0;
        int modifyNumber0To1 = 0;
        for (int i = stringChars.length - 1; i >= 0; i--) {
            if (stringChars[i] == '1') {
                if (startPlus1To0) modifyNumber1To0++;
            }
            if (stringChars[i] == '0') {
                startPlus1To0 = true;
            }
        }
        for (int i = 0; i < stringChars.length; i++) {
            if (stringChars[i] == '0') {
                if (startPlus0To1) modifyNumber0To1++;
            }
            if (stringChars[i] == '1') {
                startPlus0To1 = true;
            }
        }
        return modifyNumber1To0 < modifyNumber0To1 ? modifyNumber1To0 : modifyNumber0To1;
    }

    //通过率百分之百
    public static int getModifyNumber2(String string) {
        int numberof0 = 0;
        //算出所有0的个数。
        for (int i = 0; i < string.length(); i++) {
            if (string.charAt(i) == '0') numberof0++;
        }
        int result = numberof0;
        for (int i = 0; i < string.length(); i++) {
            if (string.charAt(i) == '0') {
                --numberof0;
                if (result > numberof0) result = numberof0;
            } else {
                ++numberof0;
            }
        }
        return result;
    }
}
